package com.atlanta.adopt;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

public class DownloadImageTask extends AsyncTask<String, Object, Bitmap> {

	@Override
	protected Bitmap doInBackground(String... url) {
		
		if(url.length <1)
			return null;
		InputStream is= null;
	    Bitmap bm= null;
	    try{
	        HttpGet httpRequest = new HttpGet(url[0]);//bitmapUrl.toURI());
	        HttpClient httpclient = new DefaultHttpClient();
	        HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

	        HttpEntity entity = response.getEntity();
	        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
	        is = bufHttpEntity.getContent();
	        bm = BitmapFactory.decodeStream(is); 
	    }catch ( MalformedURLException e ){
	        Log.d( "RemoteImageHandler", "fetchImage passed invalid URL: " + url );
	    }catch ( IOException e ){
	        Log.d( "RemoteImageHandler", "fetchImage IO exception: " + e );
	    }finally{
	        if(is!=null)try{
	            is.close();
	        }catch(IOException e){}
	    }
	    return bm;
	}

}
